package com.qingke.hibernate.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

import com.qingke.hibernate.bean.Teacher;

public class TeacherDAO extends SystemDAO{

	public TeacherDAO(Session session) {
		super(session);
	}
	public List<Teacher> list(){
		Query<Teacher> query=session.createQuery("from Teacher", Teacher.class);
		return query.getResultList();
	}
	public Teacher getTeachersById(int id){
		Query<Teacher> query=session.createQuery("from Teacher where id=:id", Teacher.class);
		query.setParameter("id", id);
//		return query.getResultList().get(0);
		return query.getSingleResult();
	}
	
	
	public List<Teacher> listTeachersByName(String name){
		Query<Teacher> query=session.createQuery("from Teacher where firstname like :name or lastname like :name", Teacher.class);
		query.setParameter("name", "%"+name+"%");
		return query.getResultList();
	}
	
	public Teacher listTeacherByPhone(String phone){
		Query<Teacher> query=session.createQuery("from Teacher where phone=:phone", Teacher.class);
		query.setParameter("phone", phone);
		return query.getSingleResult();
	}
	
	public Teacher listTeacherByEmail(String email){
		Query<Teacher> query=session.createQuery("from Teacher where email=:email", Teacher.class);
		query.setParameter("email", email);
		return query.getSingleResult();
	}
	
	public List<Teacher> listTeachersByGender(String gender){
		Query<Teacher> query=session.createQuery("from Teacher where gender=:gender", Teacher.class);
		query.setParameter("gender", gender);
		return query.getResultList();
	}
	
	public List<Teacher> listTeachersByAge(int age){
		Query<Teacher> query=session.createQuery("from Teacher where age=:age", Teacher.class);
		query.setParameter("age", age);
		return query.getResultList();
	}
}
